package com.um.jdy.ykc.rpc.service.impl;

import com.um.jdy.rpc.ev.confirmation.BaseConfirmation;
import com.um.jdy.ykc.command.YkcCommand;
import com.um.jdy.ykc.enums.CommandEnum;
import com.um.jdy.ykc.utils.RadixsUtil;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.slf4j.Slf4j;

/**
 * @author 邓南辉
 * 日期 2022/8/27
 * 版权所有 广州优匠科技有限公司
 */
@Slf4j
public class RpcService {

    protected <T extends BaseConfirmation> T delayGetConfirmation(ChannelHandlerContext context, YkcCommand request, RpcCallback callback){
        T resp = null;
        int count = 0;


        while ((resp == null || count == 0) && count < 10){

            try {
                Thread.sleep(2000);
                count ++;
                String key = context.channel().id().asLongText()+"_"+request.getCommand()+"_"+request.getSerial();
                log.info("RPC callback key : {}",key);
                resp = (T) callback.getConfirmation(key);
                log.info("RPC callback key : {}-{}", key, resp);
            }catch (Exception ex){
                log.error("DelayGetRespException", ex);
            }
        }

        return resp;
    }

    public interface RpcCallback{
        BaseConfirmation getConfirmation(String key);
    }
}
